<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Code by TinhNQ
 * 2014
 **/
class Regions_model extends MY_Model {

    protected $region;

    function __construct(){
        parent::__construct();
    }

    function table_name(){
        return "regions";
    }

    function get_regions($filters = array(),$counting = false){
        if (isset($this->CI->cache)){
            $caching = true;
            $cache_key = 'ci--Region_'.md5(serialize($filters));
            if ($counting){
                $cache_key = 'ci--Region_COUNT_'.md5(serialize($filters));
            }
            if ($return = $this->cache->file->get($cache_key)) {
                return ($return == 'empty_cache') ? FALSE : $return;
            }
        }else{
            $caching = false;
        }

        $this->db->select('*');
        $this->db->from('regions');
        $param = array();

        if (isset($filters['region_id'])) {
            $this->db->where('region_id', $filters['region_id']);
        }
        if (isset($filters['name'])) {
            $this->db->like('name', $filters['name']);
        }
        if (isset($filters['alias'])) {
            $this->db->where('alias', $filters['alias']);
        }
        if (isset($filters['status'])) {
            $this->db->where('status', $filters['status']);
        }


        if (isset($filters['limit'])) {
            $offset = (isset($filters['page'])) ? $filters['page'] : 0;
            $this->db->limit($filters['limit'], $offset);
        }

        $result = $this->db->get();

        if($counting !=false){
            $rows = $result->num_rows();
            $result->free_result();

            if ($caching == TRUE) {
                $this->CI->cache->file->save($cache_key, $rows, (5*60));
            }

            return $rows;
        }
        if ($result->num_rows() == 0) {
            return false;
        }
        $region = array();
        foreach ($result->result_array() as $row ){
            $row['link'] = create_url('region/'.$row['alias'],array('rewrite'=>true));
            $region[] = $row;
        }
        if ($caching){
            $this->CI->cache->file->save($cache_key,$region);
        }
        $result->free_result();

        return $region;
    }

    function get_count($filter = array()){
        return $this->get_regions($filter,true);
    }

    function get_region($regionId){
        if (!isset($this->region[$regionId])){
            $region = $this->get_regions(array('region_id'=>$regionId));
            $this->region[$regionId] = $region;
        }else{
            $region = $this->region[$regionId];
        }
        if (empty($region)) {
            return false;
        }
        return $region[0];
    }

    function insert_region($data){
        $_region_id = $this->save($data);
        return $_region_id;
    }

    function update_region($data, $region_id){
        return $this->update($data, array('region_id' => $region_id));
    }

    function delete_region($region_id) {
        return $this->delete(array('region_id'=>$region_id));
    }

    function change_status_region($region_id) {
        $status = $this->db->select('status')->from('regions')->where('region_id', $region_id)->get()->result_array();

        if ($status[0]['status'] == 1) {
            $change = 0;
        } else {
            $change = 1;
        }
        return $this->update(array('status' => $change), array('region_id' => $region_id));
    }

    function get_region_by_alias($region_alias){
        $region = $this->get_regions(array('alias'=>$region_alias),false);
        if ($region){
            return $region[0];
        }
        return false;
    }

    function get_regions_options($filters=array()){
        $regions_options = array();
        $regions = $this->get_regions($filters);
        if (is_array($regions) && count($regions)){
            foreach ($regions as $region){
                $regions_options[$region['region_id']] = $region['name'];
            }
        }
        return $regions_options;
    }
}
?>